package com.wva.gateway.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.reactive.CorsWebFilter;
import org.springframework.web.cors.reactive.UrlBasedCorsConfigurationSource;

/**
 * @Author: hong
 * @Date: 2025/2/20 02:05:03
 */
@Configuration
public class CorsConfig {
    @Bean
    @Order(-101)
    public CorsWebFilter corsWebFilter() {
        CorsConfiguration config = new CorsConfiguration();
        config.addAllowedOrigin("*"); // 允许来自 http://example.com 的请求
        config.addAllowedMethod("*"); // 允许 GET 请求方法
        // config.addAllowedMethod("POST"); // 允许 POST 请求方法
        config.addAllowedHeader("*"); // 允许所有头信息
        // config.setAllowCredentials(true); // 是否允许发送 cookie
        config.setMaxAge(168000L); // 预检请求的有效期，单位为秒
        UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
        source.registerCorsConfiguration("/**", config); // 对所有路径应用 CORS 配置
        return new CorsWebFilter(source);
    }
}
